<template>
  <div class="pt-8 pr-16">
    <el-form ref="form" :model="form" label-width="160px">
      <el-form-item label="请选择发送对象">
        <el-input v-model="names" @click.native="showUsers"></el-input>
      </el-form-item>
      <el-form-item label="消息内容">
        <el-input v-model="form.content"></el-input>
      </el-form-item>
      <el-form-item label="">
        <el-button type="primary" @click="send">发送</el-button>
      </el-form-item>
    </el-form>
    <user-list ref="users" @set-owner="setUsers" />
  </div>
</template>

<script>
import userList from "./users";

export default {
  components: { userList },
  data() {
    return {
      form: {
        userIds: [],
        content: "",
      },
      names: "",
    };
  },
  methods: {
    init() {
      this.form = {
        userIds: [],
        content: "",
      };
      this.names = "";
    },
    setUsers(users) {
      if (users.length) {
        this.names = users.map((item) => item.displayName).join(", ");
        this.form.userIds = users.map((item) => item.uid);
      }
    },
    showUsers() {
      this.$refs.users.open();
    },
    send() {
      const content = this.form.content.trim();
      if (!content) return;
      this.axios.post("message/multicast", this.form).then((res) => {
        if (res.retCode != 0) {
          return this.$message.error(res.retMsg);
        }
        this.init();
        this.$message.success("操作成功!");
      });
    },
  },
};
</script>

<style>
</style>